<template>
  <view class="form-item">
    <text class="form-label">链接</text>
    <view v-for="(link, index) in links" :key="index" class="link-item">
      <view class="input-wrapper">
        <input v-model="links[index]" class="link-input" placeholder="请输入链接"
          @input="handleInput" />
      </view>
      <button class="delete-button" @click.stop="handleDelete(index)">
        <image src="/static/icons/close.svg" class="delete-icon" />
      </button>
    </view>
    <button class="add-button" @click="handleAdd">添加链接</button>
  </view>
</template>

<script setup>
import { ref, watch } from 'vue'

const props = defineProps({
  modelValue: {
    type: Array,
    default: () => ['']
  }
})

const emit = defineEmits(['update:modelValue'])

const links = ref(props.modelValue)

watch(() => props.modelValue, (newVal) => {
  links.value = newVal
})

const handleInput = () => {
  emit('update:modelValue', links.value)
}

const handleAdd = () => {
  links.value.push('')
  emit('update:modelValue', links.value)
}

const handleDelete = (index) => {
  links.value.splice(index, 1)
  emit('update:modelValue', links.value)
}
</script>

<style lang="scss" scoped>
.link-item {
  display: flex;
  align-items: center;
  gap: 16rpx;
  margin-bottom: 24rpx;
  background: #fff;
  border-radius: 12rpx;
  padding: 16rpx;
  box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.05);

  .input-wrapper {
    flex: 1;
    position: relative;
  }

  .link-input {
    // width: 100%;
    height: 80rpx;
    padding: 0 24rpx;
    border: 2rpx solid #dcdfe6;
    border-radius: 8rpx;
    font-size: 28rpx;
    background: #fff;
  }

  .delete-button {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 48rpx;
    height: 48rpx;
    padding: 0;
    background: rgba(0, 0, 0, 0.05);
    border: none;
    border-radius: 50%;
    transition: all 0.3s ease;

    &:active {
      background: rgba(0, 0, 0, 0.1);
    }

    .delete-icon {
      width: 24rpx;
      height: 24rpx;
    }
  }
}
</style>